/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.j2ee.compumake.filtros;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/************************************************************************
 **                                                                    **
 ** Nombre: FiltroLoginUser.java                                           **
 ** Version: v1.0.0                                                    **
 ** Autor: Eric Huerta                                                 **
 ** Creado: 22/04/2013                                                 **
 ** Modificado:                                                        **
 ** Diagrama:                                                          **
 ** Caso de uso:                                                       **
 **                                                                    **
 ***********************************************************************/
public class FiltroLoginUser implements Filter {
    
    private static final boolean debug = false;
    private FilterConfig config = null;
    
    public FiltroLoginUser() {
    }    
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
        
        HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		if(req.getSession().getAttribute("banderaTipo")==null){
			resp.sendRedirect("index.jsp");
		}else{
			long login=Long.parseLong(req.getSession().getAttribute("banderaTipo").toString());
			if(login==1){
				chain.doFilter(request, response);	
			}else{
				resp.sendRedirect("index.jsp");
			}
		}
    }

   
    @Override
    public void destroy() {}


    @Override
    public void init(FilterConfig filterConfig) {        
        this.config = filterConfig;
        if (filterConfig != null) {
            if (debug) {                
                log("FiltroLoguin: Initializing filter");
            }
        }
    }

    public void log(String msg) {
        config.getServletContext().log(msg);        
    }
   
}
